package com.anan.service.impl;

import com.anan.constants.Constants;
import com.anan.mapper.ImgResourceMapper;
import com.anan.mapper.OrderMapper;
import com.anan.mapper.ProductMapper;
import com.anan.mapper.UserProdRelateMapper;
import com.anan.pojo.bo.ImgInfosBO;
import com.anan.pojo.domain.ImgResourceDO;
import com.anan.pojo.domain.OrderDO;
import com.anan.pojo.domain.ProductDO;
import com.anan.pojo.dto.BuyProductDTO;
import com.anan.pojo.dto.ImgDTO;
import com.anan.pojo.dto.ProductDTO;
import com.anan.pojo.dto.UpdateStatusDTO;
import com.anan.pojo.vo.OrderVO;
import com.anan.pojo.vo.ProductInfosVO;
import com.anan.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @author anan
 * @date 2023/05/08/08:53
 **/
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Resource
    UserProdRelateMapper prodRelateMapper;

    @Resource
    ImgResourceMapper imgResourceMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    public boolean addProducts(ProductDTO productDTO) {
        if (null != productDTO) {
            ProductDO productDO = new ProductDO();
            BeanUtils.copyProperties(productDTO, productDO);
            productDO.setAdminStatus(Constants.ADMIN_STATUS.NO_PASS);
            if (productMapper.addProductDOs(productDO)) {
                //添加用户和商品关系
                boolean flag = prodRelateMapper.addUserProdRelate(productDTO.getStudentId(), null, productDO.getProductId());
                if (flag) {
                    ImgDTO imgDTO = new ImgDTO();
                    List<ImgInfosBO> imgInfosBOs = productDTO.getImgInfosBOs();
                    imgInfosBOs.forEach(imgInfosBO -> {
                        imgInfosBO.setRelateId(productDO.getProductId());
                        imgInfosBO.setImgType(Constants.TYPE.PRODUCT);
                    });
                    imgDTO.setImgInfosBOs(imgInfosBOs);
                    return imgResourceMapper.addImgs(imgDTO);
                }
            }
        }
        return false;
    }

    @Override
    public ProductInfosVO getAllProductInfos(Integer adminStatus, Integer productStatus) {
        ProductInfosVO productInfosVO = new ProductInfosVO();
        List<ProductDTO> productDTOS = new ArrayList<>();
        productInfosVO.setProductDTOS(productDTOS);
        List<ProductDO> products = productMapper.getAllProducts(productStatus, adminStatus, 0);

        if (null != products) {
            products.forEach(productDO -> {
                ProductDTO productDTO = new ProductDTO();
                List<ImgInfosBO> imgInfos = new ArrayList<>();
                BeanUtils.copyProperties(productDO,  productDTO);
                List<ImgResourceDO> imgResourceDOS = imgResourceMapper.getUrlByIdAndType(Constants.TYPE.PRODUCT, productDO.getProductId());
                imgResourceDOS.forEach(imgResourceDO -> {
                    ImgInfosBO imgInfosBO = new ImgInfosBO();
                    BeanUtils.copyProperties(imgResourceDO, imgInfosBO);
                    imgInfos.add(imgInfosBO);
                    productDTO.setImgInfosBOs(imgInfos);
                });
                productDTOS.add(productDTO);
            });
            return productInfosVO;
        }
        return null;
    }

    @Override
    public OrderVO buyProduct(BuyProductDTO buyProductDTO) {
        if (null != buyProductDTO) {
            //更新商品状态
            boolean flag = productMapper.updateBuyStatus(buyProductDTO.getBuyStatus(), buyProductDTO.getProductId());
            if (flag) {
                ProductDO productDO = productMapper.getProductById(buyProductDTO.getProductId());
                if (null == productDO) log.info("===== 未查到商品 =====");
                //创建订单信息
                OrderDO orderDO = new OrderDO();
                orderDO.setPrice(productDO.getPrice());
                orderDO.setProdName(productDO.getTitle());
                orderDO.setStudentId(buyProductDTO.getStudentId());
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                orderDO.setCreateDate(date);

                //保存订单信息
                orderMapper.addOrder(orderDO);
                OrderVO orderVO = new OrderVO();
                BeanUtils.copyProperties(orderDO, orderVO);
                String createDate = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss").format(date);
                orderVO.setCreateDate(createDate);
                return orderVO;
            }
        }
        return null;
    }

    @Override
    public ProductInfosVO getProductByLike(String qryName) {
        if (null != qryName) {
            ProductInfosVO productInfosVO = new ProductInfosVO();
            List<ProductDTO> productDTOS = new ArrayList<>();
            productInfosVO.setProductDTOS(productDTOS);
            List<ProductDO> products = productMapper.getProductByLike(qryName, Constants.ADMIN_STATUS.PASS, Constants.PRODUCT_STATE.SHELVES_ON, Constants.BUT_TYPE.BUY_NO);

            if (null != products) {
                products.forEach(productDO -> {
                    ProductDTO productDTO = new ProductDTO();
                    List<ImgInfosBO> imgInfos = new ArrayList<>();
                    BeanUtils.copyProperties(productDO,  productDTO);
                    List<ImgResourceDO> imgResourceDOS = imgResourceMapper.getUrlByIdAndType(Constants.TYPE.PRODUCT, productDO.getProductId());
                    imgResourceDOS.forEach(imgResourceDO -> {
                        ImgInfosBO imgInfosBO = new ImgInfosBO();
                        BeanUtils.copyProperties(imgResourceDO, imgInfosBO);
                        imgInfos.add(imgInfosBO);
                        productDTO.setImgInfosBOs(imgInfos);
                    });
                    productDTOS.add(productDTO);
                });
                return productInfosVO;
            }
        }
        return null;
    }

    public boolean updateShelvesStatus(UpdateStatusDTO updateStatusDTO) {
        if (null != updateStatusDTO) {
            return productMapper.updateShelvesStatus(updateStatusDTO.getProductId(), updateStatusDTO.getStatus());
        }
        return false;
    }
}
